package de.szut.ita13sommerfeld.blockadventure.level;

import java.awt.image.BufferedImage;
import java.util.Random;

import de.szut.ita13sommerfeld.blockadventure.controller.Controller;
import de.szut.ita13sommerfeld.blockadventure.gameentities.Block;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.Bound;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.CheckPoint;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.CoinBox;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.Dirt;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.Grass;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.MissileLauncher;
import de.szut.ita13sommerfeld.blockadventure.gameentities.blocks.Sand;
import de.szut.ita13sommerfeld.blockadventure.gameentities.character.Character;
import de.szut.ita13sommerfeld.blockadventure.window.Game;

public class LevelGenerator {
	
	Random random = Game.getRandomInstance();
	
	private Controller controller;
	private BufferedImage level;
	private int width, height;
	
	public LevelGenerator(BufferedImage level, Controller controller) {
		this.level = level;
		this.controller = controller;
	}
	
	public void generateLevel() {
		width = level.getWidth();
		height = level.getHeight();
		
		for(int x = 0; x < width; x++) {
			for(int y = 0; y < height; y++) {
				int pixel = level.getRGB(x, y);
				int red = (pixel >> 16) & 0xff;
				int green = (pixel >> 8) & 0xff;
				int blue = (pixel) & 0xff;
				LevelObject levelobject = new LevelObject(red,green,blue);
				if(LevelObject.is(levelobject, LevelObject.BLOCK_BOUNDS)) 
					controller.addEntity(new Bound(x * 32, y * 32, Block.WIDTH , Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject, LevelObject.BLOCK_DIRT)) 
					controller.addEntity(new Dirt(x * 32, y * 32, Block.WIDTH , Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject,	LevelObject.BLOCK_GRASS))
					controller.addEntity(new Grass(x * 32, y * 32, Block.WIDTH , Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject, LevelObject.BLOCK_SAND))
					controller.addEntity(new Sand(x * 32, y * 32, Block.WIDTH , Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject, LevelObject.BLOCK_SPAWNPOINT))
					controller.addEntity(new CheckPoint(x * 32, y * 32, Block.WIDTH, Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject, LevelObject.BLOCK_COINCOLLECTOR))
					controller.addEntity(new CoinBox(x * 32, y * 32, Block.WIDTH, Block.HEIGHT, new SpawnPoint(x * 32, y * 32)));
				else if(LevelObject.is(levelobject, LevelObject.BLOCK_MISSILE_LAUNCHER))
					controller.addEntity(new MissileLauncher(x * 32, y * 32, Block.WIDTH, Block.HEIGHT, new SpawnPoint(x * 32, y * 32), 0));
				else if(LevelObject.is(levelobject, LevelObject.PLAYER))
					controller.addEntity(new Character(x * 32, y * 32, Character.WIDTH, Character.HEIGHT, 0.4f, new SpawnPoint(x * 32, y * 32)));
			}
		}
		//Coin Generation
	}

	public BufferedImage getLevel() {
		return level;
	}

	public void setLevel(BufferedImage level) {
		this.level = level;
	}
}
